<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户 控制层
 * @author adela.xu 8.29
 */
class User extends My_Controller
{
    private $assign = array();
    /**
     * 默认方法
     * @author Toby.tu 2016-09-01
     */
    public function init(){
        $this->vendor('models/User_model','user_model');
    }
    /**
     * 用户列表
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $input = $this->input->post();
        $cur_page = 1;
        if(isset($input['cur_page'])){
            $cur_page = $input['cur_page'];
        }
        $search = array();
        $filter = array(
            'user_name','name','permis_id','brand_id'
        );
        foreach ($filter as $fld) {
            if(isset($input[$fld]) && trim($input[$fld]) != ''){
                $search[$fld] = $input[$fld];
            }
        }
        // $this->pagesize = 2;
        $userList = $this->user_model->getUserList($search,$cur_page,$this->pagesize);
        $user_count = $this->user_model->getUserListCount($search);
        $this->assign['userlist'] = $userList;
        $pagetitle = '';$paginate = '';
        if($user_count > 0){
            $paginate = $this->paginate($cur_page,$user_count);
            $start = (($cur_page - 1) * $this->pagesize ) + 1;
            $end = ($start+$this->pagesize-1);
            if($end > $user_count){
                $end = $user_count;
            }
            $pagetitle = $start.' ~ '.$end;
            $pagetitle .= ' &nbsp;&nbsp;&nbsp;&nbsp;共 '.$user_count.' 条';
        }
        $this->assign['paginate'] = $paginate;
        $this->assign['pagetitle'] = $pagetitle;
        //获取品牌列表
        $this->vendor('@.Brand_model');
        $brands = $this->brand_model->getAll();
        $this->assign['brand_opts'] = $brands;
        //获取权限组列表
        $this->vendor('@.Permis_model');
        $permis = $this->permis_model->getAll();
        $this->assign['permis_opts'] = $permis;
        $this->assign['search'] = $search;
        if(isset($input['ajax']) && 1 == $input['ajax']){
            $this->load->view('admin/user/list',$this->assign);
        }else{
            $this->assign['view'] = 'admin/user/index';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function create(){
        //获取品牌列表
        $this->vendor('@.Brand_model');
        $brands = $this->brand_model->getAll();
        $this->assign['brand_opts'] = $brands;
        //获取权限组列表
        $this->vendor('@.Permis_model');
        $permis = $this->permis_model->getAll();
        $this->assign['permis_opts'] = $permis;
        //获取部门列表
        $this->vendor('@.Group_model');
        $groups = $this->group_model->getAll();
        $this->assign['group_opts'] = $groups;
        //获取是否管理员选项
        $this->vendor('@.Picklist_model');
        $is_admin_opts = $this->picklist_model->getOptsByName('is_admin');
        $this->assign['is_admin_opts'] = $is_admin_opts;
        $this->assign['view'] = 'admin/user/create';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增用户组
     * @author Toby.tu 2016-08-31
     */
    public function store(){
        try {
            $input = $this->input->post();
            if(!isset($input['user'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['user']['name'])){
                throw new Exception('请输入用户名。');
            }
            if(empty($input['user']['user_name'])){
                throw new Exception('请输入用户姓名。');
            }
            if(empty($input['user']['password'])){
                throw new Exception('请输入密码。');
            }
            if(empty($input['user']['brand_id'])){
                throw new Exception('请选择品牌。');
            }
            if(empty($input['user']['permis_id'])){
                throw new Exception('请选择权限组。');
            }
            if($input['user']['password'] != $input['is_password']){
                throw new Exception('密码和确认密码不一致。');
            }
            $user = $this->user_model->getOne(['name'=>$input['user']['name']]);
            if(!empty($user)){
                throw new Exception('用户名称已存在。');
            }
            $param = $input['user'];
            $param['create_time'] = $this->phpdate->time();
            $param['update_time'] = $param['create_time'];
			$password = $input['user']['name'].$input['user']['password'];
            $param['password'] = $this->user_model->encodePasseord($password);
            $this->user_model->newData($param);
            redirect('admin/user/index');die;
        } catch (Exception $e) {
            $this->assign['user'] = $input['user'];
            //获取品牌列表
            $this->vendor('@.Brand_model');
            $brands = $this->brand_model->getAll();
            $this->assign['brand_opts'] = $brands;
            //获取权限组列表
            $this->vendor('@.Permis_model');
            $permis = $this->permis_model->getAll();
            $this->assign['permis_opts'] = $permis;
            //获取部门列表
            $this->vendor('@.Group_model');
            $groups = $this->group_model->getAll();
            $this->assign['group_opts'] = $groups;
            //获取是否管理员选项
            $this->vendor('@.Picklist_model');
            $is_admin_opts = $this->picklist_model->getOptsByName('is_admin');
            $this->assign['is_admin_opts'] = $is_admin_opts;
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/user/create';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function edit($id){
        $user = $this->user_model->getOne(['id'=>$id]);
        unset($user['password']);
        unset($user['status']);
        $this->assign['user'] = $user;
        //获取品牌列表
        $this->vendor('@.Brand_model');
        $brands = $this->brand_model->getAll();
        $this->assign['brand_opts'] = $brands;
        //获取权限组列表
        $this->vendor('@.Permis_model');
        $permis = $this->permis_model->getAll();
        $this->assign['permis_opts'] = $permis;
        //获取部门列表
        $this->vendor('@.Group_model');
        $groups = $this->group_model->getAll();
        $this->assign['group_opts'] = $groups;
        //获取是否管理员选项
        $this->vendor('@.Picklist_model');
        $is_admin_opts = $this->picklist_model->getOptsByName('is_admin');
        $this->assign['is_admin_opts'] = $is_admin_opts;
        if(1 != $this->permis_logic->getLoginUserInfo()['id']){
            unset($this->assign['is_admin_opts'][1]);//只有admin才能设置管理员
        }else if(1 == $id){
            unset($this->assign['is_admin_opts'][0]);//admin用户不能被修改为非管理员
        }
        $this->assign['id'] = $id;
        $this->assign['current_id'] = $this->permis_logic->getLoginUserInfo()['id'];
        $this->assign['view'] = 'admin/user/edit';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function update($id){
        try {
            $input = $this->input->post();
            if(!isset($input['user'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['user']['name'])){
                throw new Exception('请输入菜单姓名。');
            }
            $user = $this->user_model->getOne(['name'=>$input['user']['name']]);
            if(!empty($user) && $id != $user['id']){
                throw new Exception('用户名称已存在。');
            }
            $param = $input['user'];
            $param['update_time'] = $this->phpdate->time();
            $this->user_model->updateById($id,$param);
            redirect('admin/user/index');die;
        } catch (Exception $e) {
            $this->assign += $input['user'];
            $this->assign['id'] = $id;
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['view'] = 'admin/user/edit';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 禁用启用
     * @author Toby.tu 2016-08-31
     */
    public function enable($id){
        try {
            $input = $this->input->get();
            if(!isset($input['status'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['status'] = ($input['status']==1)?0:1;
            $this->user_model->updateById($id,$param);
            redirect('admin/user/index');die;
        } catch (Exception $e) {
            redirect('admin/user/index');die;
        }
    }
    /**
     * 删除
     * @author Toby.tu 2016-08-31
     */
    public function destroy($id){
        // $this->user_model->deleteById($id);
        redirect('admin/User/index');die;
    }
    /**
     * 重置密码
     * @author Toby.tu 2016-09-01
     */
    public function resetpwd($id){
        $this->assign['id'] = $id;
        // $user = $this->user_model->getOne(['id'=>$id]);
        // unset($user['password']);
        // unset($user['status']);
        // $this->assign['user'] = $user;
        $this->assign['is_admin'] = $this->permis_logic->isAdmin();
        $this->assign['view'] = 'admin/user/resetpwd';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 更新用户密码
     * @author Toby.tu 2016-09-01
     */
    public function updatepwd($id){
        try {
            $input = $this->input->post();
            if(!isset($input['user'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['user']['password'])){
                throw new Exception('请输入密码。');
            }
            if($input['user']['password'] != $input['is_password']){
                throw new Exception('密码和确认密码不一致。');
            }
            $user = $this->user_model->getOne([$id]);
            $is_admin = $this->permis_logic->isAdmin();
            if('yes' != $is_admin){
                if(empty($input['old_password'])){
                    throw new Exception('请输入原密码。');
                }
				$password = $user['name'].$input['old_password'];
                $old_pwd = $this->user_model->encodePasseord($password);
                if($old_pwd != $user['password']){
                    throw new Exception('用户原密码输入不正确。');
                }
            }
            $param = array();
			$password = $user['name'].$input['user']['password'];
            $param['password'] = $this->user_model->encodePasseord($password);
            $param['update_time'] = $this->phpdate->time();
            $this->user_model->updateById($id,$param);
            redirect('admin/User/index');die;
        } catch (Exception $e) {
            $this->assign = array();
            unset($this->assign['user']);
            $this->assign['error_code'] = $e->getMessage();
            $this->assign['id'] = $id;
            $this->assign['is_admin'] = $this->permis_logic->isAdmin();
            $this->assign['view'] = 'admin/user/resetpwd';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
}
